from flask_restful import Resource, marshal_with
from sqlalchemy import and_

from apps.common.result import Result
from apps.main.fields import result
from apps.main.models import Movies, area


# result = {
#     'status':'',
#     'msg':'',
#     'data':{
#         'area':{
#             'hot':[],
#             'areas':[],
#         },
#         'banners':[],
#         'movie': {
#             'hot_counts':n1,
#             'will_counts':n2,
#             'hot_movirs':[mov1,mov2,...movn],
#             'will_movies':[mov1,mov2,...movn],
#         }
#     }
#
# }
from apps.user.models import User


class HomeApi(Resource):
    @marshal_with(result)
    def get(self):
        #  获取热门电影的数量
        area.query.all()
        hot_count = Movies.query.filter(and_(Movies.flag == 1, Movies.is_delete == 0)).count()
        will_count = Movies.query.filter(and_(Movies.flag == 2, Movies.is_delete == 0)).count()

       #  获取热门电影的数据
        hot_movies =  Movies.query.filter(and_(Movies.flag == 1,Movies.is_delete == 0)).limit(5).offset(0).all()
        will_movies = Movies.query.filter(and_(Movies.flag == 2, Movies.is_delete == 0)).limit(5).offset(0).all()
        # 注意这里的字段要完全参照fields里的字段

        # 获取热门城市(过滤列,只显示热门城市的名称)
        hot_areas = area.query.filter(area.is_hot == 1).all()
        # 获取首字母开头的城市
        keys = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        normal_areas = []

        for key in keys:
            normal_areas.append({
                'first': key,
                'normal_list': area.query.filter(and_(area.first == key, area.level == 2)).all()
            })




        data = {
           'movie': {
               'hot_count': hot_count,
               'will_count': will_count,
               'hot_movies':hot_movies,
               'will_movies':will_movies,
           },
            'area': {
                'hot_areas': hot_areas,
                'normal_areas': normal_areas,
            },
       }
        return Result.get_success_result_obj(data)

    def post(self):
        pass